package com.demohomework.homeworkdemo.controller;

import com.demohomework.homeworkdemo.pojo.Notice;
import com.demohomework.homeworkdemo.pojo.Result;
import com.demohomework.homeworkdemo.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    //新增
    @PostMapping("/add")
    public Result add(@RequestBody @Validated({Notice.Add.class}) Notice notice) {
        noticeService.add(notice);
        return Result.success();
    }

    //查询通知
    @GetMapping
    public Result<List<Notice>> list() {
        List<Notice> notices = noticeService.list();
        return Result.success(notices);
    }
    //根据id查询通知
    @GetMapping("/select")
    public Result<Notice> show(Integer id ) {
        Notice a = noticeService.findId(id);
        return Result.success(a);
    }
    //更新通知
    @PutMapping("/update")
    public Result update(@RequestBody @Validated(Notice.Update.class) Notice notice){
        noticeService.update(notice);
        return Result.success();
    }
    //删除通知
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id")  Integer id){
        noticeService.delete(id);
        return Result.success();
    }
    
}
